<template>
  <div class="container">
    <div class="header-container">
      <span class="type-item">{{ displayData.matchType }} / {{ displayData.leagueName }}</span>
      <p class="header-item time" :class="{ finished: isFinished }">
        {{ isFinished ? formatTime(displayData.matchTime) + '完' : '' }}
        <span class="status" v-if="!isFinished">{{ displayData.matchStatus }}</span>
        <img
          src="@/assets/icons/score/minite.gif"
          class="minite"
          v-if="!isFinished && matchData?.sportId === 1"
        />
      </p>
      <span class="place-item">{{ displayData?.stadium_name }}</span>
    </div>
    <div class="content">
      <div class="team">
        <span class="title">{{ displayData.homeTeam.name }}</span>
        <img :src="displayData.homeTeam.logo" :alt="displayData.homeTeam.name" class="team-logo" />
      </div>
      <div class="score">
        <span class="result">{{ displayData.score.fullTime }}</span>
        <span class="half-time"
          >(半{{ displayData.score.halfTime }}
          {{ displayData.score.corner ? '角' + displayData.score.corner : '' }})</span
        >
      </div>
      <div class="team">
        <img :src="displayData.awayTeam.logo" :alt="displayData.awayTeam.name" class="team-logo" />
        <span class="title right-title">{{ displayData.awayTeam.name }}</span>
      </div>
    </div>
  </div>
</template>

<script setup>
import { inject, ref, watchEffect, computed } from 'vue'
// 导入 dayjs
import dayjs from 'dayjs'

// 注入从父组件提供的 matchData
const matchData = inject('matchData')

// 创建响应式变量来存储需要显示的数据
const displayData = ref({
  matchType: '',
  leagueName: '',
  matchTime: '',
  matchStatus: '',
  stadium_name: '',
  homeTeam: {
    name: '',
    logo: ''
  },
  awayTeam: {
    name: '',
    logo: ''
  },
  score: {
    fullTime: '',
    halfTime: '',
    corner: ''
  }
})

// 使用 watchEffect 来监听 matchData 的变化并更新 displayData
watchEffect(() => {
  if (matchData.value) {
    const data = matchData.value
    displayData.value = {
      matchType: '篮球',
      leagueName: data.league_name || '',
      matchTime: data.match_time || '',
      matchStatus: data.match_status_txt,
      stadium_name: data.stadium_name || '',
      homeTeam: {
        name: data.home_team?.name || '',
        logo: data.home_team?.logo || ''
      },
      awayTeam: {
        name: data.guest_team?.name || '',
        logo: data.guest_team?.logo || ''
      },
      score: {
        fullTime: `${data.home_total_score || 0}-${data.guest_total_score || 0}`,
        halfTime: `${data.home_half_score || 0}-${data.guest_half_score || 0}`,
        corner: ''  // 篮球比赛没有角球数据
      }
    }
  }
})

// 辅助函数：根据比赛状态代码返回对应的文字描述
function getMatchStatus(status) {
  const statusMap = {
    0: '未开始',
    1: '进行中',
    2: '已结束',
    3: '中断',
    4: '取消',
    5: '推迟'
  }
  return statusMap[status] || '未知'
}

// 时间格式化函数
const formatTime = (time, format = 'YYYY-MM-DD HH:mm') => {
  if (!time) return ''
  return dayjs(time).format(format)
}

// 修改计算属性来判断比赛是否完场
const isFinished = computed(() => {
  if (!matchData.value) return false
  return matchData.value.match_status === 3
})
</script>

<style scoped lang="scss">
.container {
  width: 100%;

  background: #fff;
  height: 132px;
}
.header-container {
  display: flex;
  justify-content: space-between;
  align-items: center;
  //   width: 100%;
  width: 1200px;
  margin: 0 auto;
  //   padding: 0 20px;
  height: 35px;
  font-family:
    PingFang SC,
    PingFang SC;
  font-weight: 400;
  font-size: 14px;
  color: #afb8cb;

  .header-item {
    flex: 1;
    text-align: center;
    position: relative;
    display: flex;
    align-items: flex-start;
    justify-content: center;
    .minite {
      width: 2px;
      margin-left: 3px;
      // position: absolute;
      // top: 0;
      // right: 0;
    }
    &.time {
      color: red;
    }
    &.finished {
      color: #afb8cb;
    }
  }
  .type-item {
    flex: 1;
  }
  .place-item {
    flex: 1;
    // white-space: nowrap;
    text-align: end;
  }
}

.content {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
  padding: 20px 0;

  .team {
    display: flex;
    // flex-direction: column;
    align-items: center;
    font-weight: 500;
    font-size: 22px;
    color: #525a79;

    .team-logo {
      width: 56px;
      height: 56px;
      object-fit: cover;
      //   margin-bottom: 10px;
    }
    .title {
      margin: 0 10px;
      min-width: 200px;
      text-align: right;
    }
    .right-title {
      text-align: left;
    }
    .event {
      font-size: 12px;
      color: #999;
    }
  }

  .score {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    // font-size: 24px;
    // font-weight: bold;
    font-weight: 400;
    font-size: 28px;
    color: #ffffff;
    width: 200px;
    height: 56px;
    background: #16b13a;
    box-shadow: 0px 0px 10px 4px rgba(22, 177, 58, 0.4);
    border-radius: 10px 10px 10px 10px;
    margin: 0 80px;
    .result {
      line-height: 28px;
    }
    .half-time {
      font-weight: 400;
      font-size: 12px;
      color: #ffffff;
    }
  }
}
</style>
